﻿using Engine.TrafficHelpers;
using System;
using System.Windows.Forms;

namespace Scenario_Editor.CustomControls
{
    public partial class CrossroadTrafficControl : UserControl
    {
        #region Private Fields

        private CrossroadTraffic crossroadTraffic;
        private bool duringControlUpdate = false;

        #endregion Private Fields

        #region Public Constructors

        public CrossroadTrafficControl()
        {
            InitializeComponent();
            this.crossroadTraffic = new CrossroadTraffic(this.checkBox_IsMainstreet.Checked,
                this.trafficControl3.GetCrossroadTrafficData(),
                this.trafficControl3.GetCrossroadTrafficData(),
                this.trafficControl3.GetCrossroadTrafficData());
            //OnValueChange(null, null);
        }

        #endregion Public Constructors

        #region Public Delegates

        public delegate void ValueChange(object sender, EventArgs e);

        #endregion Public Delegates

        #region Public Events

        public event ValueChange valueChange;

        #endregion Public Events

        #region Public Methods

        public CrossroadTraffic GetCrossroadTraffic()
        {
            return this.crossroadTraffic;
        }

        public void SetCrossroadTraffic(CrossroadTraffic crossroadTraffic)
        {
            this.duringControlUpdate = true;
            this.crossroadTraffic = crossroadTraffic;

            SetTrafficControl();

            this.duringControlUpdate = false;

            if (valueChange != null)
                valueChange(this, null);
        }

        #endregion Public Methods

        #region Private Methods

        private void OnLaneSelectionChanged(object sender, EventArgs e)
        {
            SetTrafficControl();
        }

        private void OnValueChange(object sender, EventArgs e)
        {
            if (!this.duringControlUpdate)
            {
                this.crossroadTraffic.IsMainstreet = this.checkBox_IsMainstreet.Checked;

                if (this.radioButton_Forward.Checked)
                    this.crossroadTraffic.LaneForward = this.trafficControl3.GetCrossroadTrafficData();
                if (this.radioButton_Left.Checked)
                    this.crossroadTraffic.LaneLeft = this.trafficControl3.GetCrossroadTrafficData();
                if (this.radioButton_Right.Checked)
                    this.crossroadTraffic.LaneRight = this.trafficControl3.GetCrossroadTrafficData();

                if (valueChange != null)
                    valueChange(this, null);
            }
        }

        private void SetTrafficControl()
        {
            this.checkBox_IsMainstreet.Checked = this.crossroadTraffic.IsMainstreet;

            if (this.radioButton_Forward.Checked)
                this.trafficControl3.SetCrossroadTrafficData(this.crossroadTraffic.LaneForward);
            if (this.radioButton_Left.Checked)
                this.trafficControl3.SetCrossroadTrafficData(this.crossroadTraffic.LaneLeft);
            if (this.radioButton_Right.Checked)
                this.trafficControl3.SetCrossroadTrafficData(this.crossroadTraffic.LaneRight);
        }

        #endregion Private Methods
    }
}